//
//  OrderDetailViewController.m
//  YJlandlord
//
//  Created by 文彬 on 2018/6/26.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "OrderInfoTableViewCell.h"
#import "ContactTableViewCell.h"

@interface OrderDetailViewController ()<UITableViewDelegate,
    UITableViewDataSource>

@property (strong, nonatomic) UITableView *listTableView;

@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"订单详情";
    
    [self initPageControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark 页面相关
- (void)initPageControl{
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@-50);
    }];
    
    [self addTableHeaderView];
    
    [self addTableViewFootView];
    
    [self addOperationView];
}

- (void)addTableHeaderView{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80)];
    headView.backgroundColor = [UIColor whiteColor];
    self.listTableView.tableHeaderView = headView;
    UILabel *statueLabel = [[UILabel alloc]init];
    statueLabel.font = [UIFont systemFontOfSize:16];
    statueLabel.textColor = kTitleColor;
    statueLabel.text = @"已过期";
    [headView addSubview:statueLabel];
    [statueLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@15);
        make.width.equalTo(@200);
        make.height.equalTo(@25);
    }];
    
    UILabel *messLabel = [[UILabel alloc]init];
    messLabel.font = [UIFont systemFontOfSize:15];
    messLabel.textColor = kSecTitleColor;
    messLabel.text = @"未付款已过期";
    [headView addSubview:messLabel];
    [messLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.bottom.equalTo(@-15);
        make.width.equalTo(@200);
        make.height.equalTo(@25);
    }];
    
    UIImageView *phoneImgView = [[UIImageView alloc]init];
    phoneImgView.image = [UIImage imageNamed:@"ic_message"];
    [headView addSubview:phoneImgView];
    [phoneImgView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@25);
        make.height.equalTo(@25);
        make.right.equalTo(@-90);
        make.centerY.equalTo(headView.centerY);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"联系客服" forState:UIControlStateNormal];
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 0);
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitleColor:kMainOriginColor forState:UIControlStateNormal];
    [headView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.width.equalTo(@120);
        make.height.equalTo(@30);
        make.centerY.equalTo(headView.centerY);
    }];
}

- (void)addTableViewFootView{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    footView.backgroundColor = [UIColor whiteColor];
    self.listTableView.tableFooterView = footView;
    
    UILabel *messLabel = [[UILabel alloc]init];
    messLabel.font = [UIFont systemFontOfSize:15];
    messLabel.textColor = kTitleColor;
    messLabel.adjustsFontSizeToFitWidth = YES;
    messLabel.numberOfLines = 0;
    messLabel.text = @"温馨提示:\n需2018-12-12 14:00前办理入住，早到可能需要等待\n需2018-12-12 12:00之前办理退房，如需延迟退房请与商户协商\n未到店或未入住时，请勿轻易线下转账方式支付押金或其他费用，以免上当受骗";
    [UILabel changeLineSpaceForLabel:messLabel WithSpace:6];
    [footView addSubview:messLabel];
    [messLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@10);
        make.right.bottom.equalTo(@-10);
    }];
}

- (void)addOperationView{
    
    UIView *opeView = [[UIView alloc]init];
    opeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:opeView];
    [opeView makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.height.equalTo(@45);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"重新下单" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.layer.cornerRadius = 4;
    button.backgroundColor = kMainOriginColor;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [opeView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.width.equalTo(@90);
        make.height.equalTo(@35);
        make.centerY.equalTo(opeView.centerY);
    }];
    
    UIButton *delebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [delebutton setTitle:@"删除订单" forState:UIControlStateNormal];
    delebutton.titleLabel.font = [UIFont systemFontOfSize:15];
    delebutton.layer.cornerRadius = 4;
    [delebutton setTitleColor:kTitleColor forState:UIControlStateNormal];
    delebutton.layer.borderColor = kSecTitleColor.CGColor;
    delebutton.layer.borderWidth = 0.5;
    [opeView addSubview:delebutton];
    [delebutton makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(button.left).offset(-20);
        make.width.equalTo(@90);
        make.height.equalTo(@35);
        make.centerY.equalTo(opeView.centerY);
    }];
    
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 7;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0||section==1) {
        return 1;
    }
    else if (section==2) {
        return 5+2; //上下各留一行空白
    }
    else if(section==3){
        return 2;
    }
    else if(section==6)
    {
        return 3;
    }
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 100;
    }
    else if(indexPath.section==1){
        return 80;
    }
    else if(indexPath.section==2){
        if (indexPath.row==0||indexPath.row==6) { //上下空白行
            return 10;
        }
    }
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
        OrderInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderInfoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
        return cell;
    }
    else if(indexPath.section==1){
        
        ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.headerImgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
        return cell;
    }
    static NSString *iden = @"celliden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = kTitleColor;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    cell.textLabel.text = @"";
    cell.detailTextLabel.text = @"";
    if (indexPath.section==2) {
        if (indexPath.row==1) {
            cell.textLabel.text = @"房费";
            cell.detailTextLabel.text = @"￥500.00";
        }
        else if (indexPath.row==2) {
            cell.textLabel.text = @"保险费用";
            cell.detailTextLabel.text = @"￥0.00";
        }
        else if (indexPath.row==3) {
            cell.textLabel.text = @"订单总额";
            cell.detailTextLabel.text = @"￥500.00";
        }
        else if (indexPath.row==4) {
            cell.textLabel.text = @"押金";
            cell.detailTextLabel.text = @"￥200.00";
        }
        else if (indexPath.row==5) {
            cell.textLabel.text = @"线上支付";
            cell.detailTextLabel.text = @"￥500.00";
        }
    }
    else if(indexPath.section==3){
        if (indexPath.row==0) {
            cell.textLabel.text = @"订单编号:201828374747";
        }
        else if (indexPath.row==1) {
            cell.textLabel.text = @"下单时间:2018年12月11日   14:32:44";
        }
    }
    else if(indexPath.section==4){
        if (indexPath.row==0) {
            cell.textLabel.text = @"入住信息（2人）";
        }
        else if (indexPath.row==1) {
            cell.textLabel.text = @"小黑 4302211988100881634";
            cell.detailTextLabel.text = @"验证成功";
            cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        }
    }
    else if(indexPath.section==5){
        if (indexPath.row==0) {
            cell.textLabel.text = @"保险信息";
        }
        else if (indexPath.row==1) {
            cell.textLabel.text = @"受保人：小黑";
            cell.detailTextLabel.text = @"验证成功";
            cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        }
    }
    else if(indexPath.section==6){
        if (indexPath.row==0) {
            cell.textLabel.text = @"联系人：小黑";
        }
        else if (indexPath.row==1) {
            cell.textLabel.text = @"手机号：1516377373";
        }
        else if (indexPath.row==2) {
            cell.textLabel.text = @"备注:xxx";
        }
    }
    return cell;
    
}

#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorColor = kLineColor;
        _listTableView.backgroundColor = [UIColor clearColor];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_listTableView registerNib:[UINib nibWithNibName:@"OrderInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"OrderInfoTableViewCell"];
       [_listTableView registerNib:[UINib nibWithNibName:@"ContactTableViewCell" bundle:nil] forCellReuseIdentifier:@"ContactTableViewCell"];
        
    }
    
    return _listTableView;
}


@end
